package com.ruoyi.journal.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.journal.domain.*;
import com.ruoyi.journal.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 现金日记账Controller
 * 
 * @author ruoyi
 * @date 2025-02-25
 */
@RestController
@RequestMapping("/journal")
public class BfxCashJournalController extends BaseController
{
    @Autowired
    private IBfxCashJournalService bfxCashJournalService;

    @Autowired
    private IBfxCategoriesService bfxCategoriesService;

    @Autowired
    private IBfxProjectService bfxProjectService;

    @Autowired
    private IBfxPaymentService bfxPaymentService;
    @Autowired
    private IBfxDepartmengtService bfxDepartmengtService;
    @Autowired
    private IBfxAccountService bfxAccountService;

    @Autowired
    private IBfxCashJournalService bfxCashJournalServiceImpl;


    /**
     * 查询现金日记账列表
     */
    @RequiresPermissions("journal:journal:list")
    @GetMapping("/list")
    public TableDataInfo list(BfxCashJournal bfxCashJournal)
    {
        startPage();
        List<BfxCashJournal> list = bfxCashJournalService.selectBfxCashJournalList(bfxCashJournal);
        return getDataTable(list);
    }

    /**
     * 导出现金日记账列表
     */
    @RequiresPermissions("journal:journal:export")
    @Log(title = "现金日记账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BfxCashJournal bfxCashJournal)
    {
        List<BfxCashJournal> list = bfxCashJournalService.selectBfxCashJournalList(bfxCashJournal);
        ExcelUtil<BfxCashJournal> util = new ExcelUtil<BfxCashJournal>(BfxCashJournal.class);
        util.exportExcel(response, list, "现金日记账数据");
    }

    /**
     * 获取现金日记账详细信息
     */
    @RequiresPermissions("journal:journal:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bfxCashJournalService.selectBfxCashJournalById(id));
    }

    /**
     * 新增现金日记账
     */
    @RequiresPermissions("journal:journal:add")
    @Log(title = "现金日记账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BfxCashJournal bfxCashJournal)
    {
        return toAjax(bfxCashJournalService.insertBfxCashJournal(bfxCashJournal));
    }

    /**
     * 修改现金日记账
     */
    @RequiresPermissions("journal:journal:edit")
    @Log(title = "现金日记账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BfxCashJournal bfxCashJournal)
    {
        return toAjax(bfxCashJournalService.updateBfxCashJournal(bfxCashJournal));
    }

    /**
     * 删除现金日记账
     */
    @RequiresPermissions("journal:journal:remove")
    @Log(title = "现金日记账", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bfxCashJournalService.deleteBfxCashJournalByIds(ids));
    }
    /**
     * 查看收入支出
     */
    @RequiresPermissions("journal:journal:listCategories")
    @PostMapping("/listCategories")
    public TableDataInfo listCategories()
    {
        List<BfxCategories> list = bfxCategoriesService.selectBfxCategoriesList(null);
        return getDataTable(list);
    }
    /**
     * 查询所有部门表列表
     */
    @RequiresPermissions("journal:journal:listDepartmengt")
    @PostMapping("/listDepartmengt")
    public TableDataInfo listDepartmengt()
    {
        List<BfxDepartmengt> list = bfxDepartmengtService.selectBfxDepartmengtList(null);
        return getDataTable(list);
    }

    /**
     * 查看所有结算方式
     */
    @RequiresPermissions("journal:journal:listPayment")
    @PostMapping("/listPayment")
    public TableDataInfo listPayment()
    {
        List<BfxPayment> list = bfxPaymentService.selectBfxPaymentList(null);
        return getDataTable(list);
    }
    /**
     * 查询所有项目表
     */
    @RequiresPermissions("journal:journal:listProject")
    @PostMapping("/listProject")
    public TableDataInfo listProject()
    {
        startPage();
        List<BfxProject> list = bfxProjectService.selectBfxProjectList(null);
        return getDataTable(list);
    }

    /**
     * 查询账户列表
     */
    @RequiresPermissions("journal:journal:listAcount")
    @PostMapping("/listAcount")
    public TableDataInfo listAcount()
    {
        List<BfxAccount> list = bfxAccountService.selectBfxAccountList(null);
        return getDataTable(list);
    }
    /**
     * 现金日记账导入
     */
    @RequiresPermissions("journal:journal:RjImport")
    @Log(title = "现金日记账导入", businessType = BusinessType.IMPORT)
    @PostMapping("/RjImport")
    public AjaxResult importData(@RequestParam(value = "file",required = false) MultipartFile file,@RequestParam(value = "accountId",required = false) Integer accountId) {
        try {
            // 使用 Ruoyi 提供的 ExcelUtil 解析 Excel 文件
            ExcelUtil<BfxCashJournal> util = new ExcelUtil<>(BfxCashJournal.class);
            List<BfxCashJournal> BfxCashJournalList = util.importExcel(file.getInputStream());

            // 处理导入的数据（如插入数据库）
            for (BfxCashJournal BfxCashJournal : BfxCashJournalList) {
                // 这里可以添加业务逻辑，如校验数据、插入数据库等
//            bfxCashJournalMapper.insertBfxCashJournal(BfxCashJournal);
                BfxCashJournal.setAccountId(accountId);
                bfxCashJournalServiceImpl.insertBfxCashJournal(BfxCashJournal);
                System.out.println("导入的日记账数据：" + BfxCashJournal);
            }
            return success("导入成功");
        } catch (Exception e) {
            return error("导入失败：" + e.getMessage());
        }
    }



}
